package com.zj.processors;

import com.zj.bean.TableColumnInfo;
import com.zj.bean.TableInfo;
import com.zj.job.CodeSnippetJob;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;

public class BaseProcessor {

    protected String convertDomainName(String tableName){
        String className="";
        String[] array = tableName.split("_");
        for (int i = 0; i < array.length; i++) {
            className += array[i].substring(0, 1).toUpperCase() + array[i].substring(1);
        }
        return className;
    }

    protected String convertServiceName(String tableName){
        String className="";
        String[] array = tableName.split("_");
        for (int i = 1; i < array.length; i++) {
            className += array[i].substring(0, 1).toUpperCase() + array[i].substring(1);
        }
        return className;
    }

    protected String lowFirstChar(String tempStr){
        char[] cs=tempStr.toCharArray();
        cs[0]+=32;
        return String.valueOf(cs);
    }
    protected String upFirstChar(String tempStr){
        char[] cs=tempStr.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }

    protected Template loadTemplate(String templatePath) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        Template vmTemplate = ve.getTemplate(templatePath,"utf-8");
        return vmTemplate;
    }

    protected String simpleProcess(TableInfo tableInfo, CodeSnippetJob codeSnippetJob,String template){
        Template vmTemplate = loadTemplate(template);
        for (TableColumnInfo tableColumnInfo : tableInfo.getTableColumnInfoList()) {
            VelocityContext context = new VelocityContext();
            context.put("comment",tableColumnInfo.getComment());
            context.put("fieldName",lowFirstChar(convertDomainName(tableColumnInfo.getName())));
            StringWriter stringWriter = new StringWriter();
            vmTemplate.merge(context,stringWriter);
            codeSnippetJob.log(stringWriter.toString());
        }
        return "ok";
    }

    protected void writeFile(VelocityContext context,String template,String filePath)throws Exception{
        Template vmTemplate = loadTemplate(template);
        FileWriter writer=new FileWriter(filePath);
        vmTemplate.merge(context, writer);
        writer.flush();
        writer.close();
    }

    protected void writeFile(VelocityContext context,String template,File file)throws Exception{
        Template vmTemplate = loadTemplate(template);
        FileWriter writer=new FileWriter(file);
        vmTemplate.merge(context, writer);
        writer.flush();
        writer.close();
    }
}
